package com.lijiafang.controller;

import com.lijiafang.constant.JumpViewConstants;
import com.lijiafang.entity.User;
import com.lijiafang.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName:LoginController
 * @Description: 登录模块
 * @Author: yaoyonghao
 * @Date: 2019/3/4、16:20
 */
@Controller
public class LoginController {
    private Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private LoginService loginService;

    /**
     * @param
     * @return
     * @Title
     * @Description: 首页
     * @author yaoyonghao
     * @date 2019/3/11 17:19
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return JumpViewConstants.LOGIN;
    }

    /**
     * @param
     * @return
     * @Title
     * @Description: 登录验证
     * @author yaoyonghao
     * @date 2019/3/11 17:19
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestParam(value = "userName") String userName,
                        @RequestParam(value = "password") String password,
                        Model model, HttpServletRequest request) {
        User user = new User();
        if (!StringUtils.isEmpty(userName) && !StringUtils.isEmpty(password)) {
            user.setUserName(userName);
            user.setPassword(password);
        } else {
            return "redirect:index";
        }
        User userInfo = loginService.userLogin(user);
        if (userInfo != null) {
            request.getSession(true).setAttribute("userInfo", userInfo);
            return "redirect:main";
        } else {
            return "redirect:index";
        }
    }

    /**
     * @param
     * @return
     * @Title
     * @Description: 首页
     * @author yaoyonghao
     * @date 2019/3/12 13:20
     */
    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String userLogin(Model model, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("userInfo");
        if (user != null) {
            //TODO 动态的列表显示
        }
        return JumpViewConstants.WORKBENCH_MAIN;
    }
    /**
     * @param
     * @return
     * @Title
     * @Description:退出
     * @author yaoyonghao
     * @date 2019/3/12 16:17
     */
    @RequestMapping(value = "/loginOut", method = RequestMethod.POST)
    public String loginOut(HttpServletRequest request) {
        request.getSession().removeAttribute("userInfo");
        return "redirect:index";

    }
}